////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2012 Kentaro Sekimoto  All rights reserved.
////////////////////////////////////////////////////////////////////////////

#include <tinyhal.h>
#include "..\FM3.h"

BOOL CPU_Initialize()
{
    NATIVE_PROFILE_HAL_PROCESSOR_POWER();
    CPU_INTC_Initialize();
    return TRUE;
}

void CPU_ChangePowerLevel(POWER_LEVEL level)
{
    switch(level)
    {
        case POWER_LEVEL__MID_POWER:
            break;
        case POWER_LEVEL__LOW_POWER:
            break;
        case POWER_LEVEL__HIGH_POWER:
        default:
            break;
    }
}

void CPU_Sleep( SLEEP_LEVEL level, UINT64 wakeEvents )
{
    NATIVE_PROFILE_HAL_PROCESSOR_POWER();
#if 0
    // if using CPU_Sleep, JTAG debugger can't be used.
    // Because CPU clock goes down and JTAG debugger can't synchronize.
    switch(level)
    {
        case SLEEP_LEVEL__DEEP_SLEEP:
            SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
            break;
        case SLEEP_LEVEL__OFF:
            SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
            break;            
        default:
            SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;
            break;
    }
    __asm__ ("wfi");
#endif
}

void CPU_Halt()  // unrecoverable error
{
    NATIVE_PROFILE_HAL_PROCESSOR_POWER();
    lcd_printf("CPU_Halt()\r\n");
    while(1);
}

void CPU_Reset()
{
    NATIVE_PROFILE_HAL_PROCESSOR_POWER();
    SCB->AIRCR = (0x5FA << SCB_AIRCR_VECTKEY_Pos)
               | (1 << SCB_AIRCR_SYSRESETREQ_Pos);
    while(1);
}

BOOL CPU_IsSoftRebootSupported ()
{
    NATIVE_PROFILE_HAL_PROCESSOR_POWER();
    return TRUE;
}

void HAL_AssertEx()
{
    lcd_printf("_HAL_AssertEx()\r\n");
    //__asm volatile("    bkpt #0");
    // infinite loop
    __asm volatile("l2:");
    __asm volatile("    b    l2");
}
